home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / util / blank / BeyondTheDark.lha / BeyondTheDark / Developer / Source / Blot / Blot.c next >
C/C++ Source or Header  |  1995-03-24  |  8KB  |  363 lines

  1. /* blot Library */
  2.  
  3. #include <exec/memory.h>
  4. #include <exec/execbase.h>
  5. #include <libraries/iffparse.h>
  6. #include <utility/tagitem.h>
  7. #include <graphics/gfxbase.h>
  8. #include <intuition/intuitionbase.h>
  9.  
  10. #include <clib/macros.h>
  11.  
  12. #define __USE_SYSBASE 42
  13.  
  14. #include <proto/exec.h>
  15. #include <proto/graphics.h>
  16. #include <proto/intuition.h>
  17. #include <proto/utility.h>
  18.  
  19. #include <math.h>
  20.  
  21. #include <BTD.h>
  22.  
  23. struct IntuitionBase *IntuitionBase;
  24. struct GfxBase *GfxBase;
  25. struct Library *UtilityBase;
  26.  
  27. /* #define DEBUG YES */
  28.  
  29. #ifdef DEBUG 
  30.  
  31. void KPrintF(char *,...);
  32.  
  33. #define DEBUG_PRINTF(a,b)  KPrintF(a,b);
  34. #define DEBUG_PRINT(a)     KPrintF(a)
  35. #else
  36. #define DEBUG_PRINTF(a,b)
  37. #define DEBUG_PRINT(a)
  38. #endif
  39.  
  40.  
  41. #define QTAG(o) (BTD_Client+(o))
  42.  
  43. #define BP_Seconds QTAG(0)
  44. #define BP_Colors  QTAG(2)
  45. #define BP_Zoom    QTAG(3)
  46.  
  47. #define MAX_SECONDS 2048L /* seconds until a new graphic is plotted */
  48. #define DEF_SECONDS 10L
  49.  
  50. #define DEF_COLORS 8L
  51. #define MAX_COLORS 255L
  52.  
  53. #define DEF_ZOOM 7L
  54. #define MAX_ZOOM 20L
  55.  
  56. struct BTDInteger blotIntParams[] =
  57.  {
  58.   BP_Seconds,"Patternchange",BTDPT_INTEGER,DEF_SECONDS,1L,MAX_SECONDS,TRUE,
  59.   BP_Colors,"Colors",BTDPT_INTEGER,DEF_COLORS,4L,MAX_COLORS,TRUE,
  60.   BP_Zoom,"Grow leafs",BTDPT_INTEGER,DEF_ZOOM,4L,MAX_ZOOM,TRUE
  61.  };
  62.  
  63. struct BTDNode *blotParams[] = 
  64.  {
  65.   &blotIntParams[0].BI_Node,
  66.   &blotIntParams[1].BI_Node,
  67.   &blotIntParams[2].BI_Node,NULL
  68.  };
  69.  
  70. struct BTDInfo blotInfo =
  71.  {
  72.   BTDI_Revision,MAKE_ID('B','L','O','T'),
  73.   "The Blots","The return of the Blots\nYet another standard modul","Markus Illenseer 1995",
  74.   blotParams
  75.  };
  76.  
  77. char MyBlankerName[] = "blot.btd";
  78. char MyBlankerID[]   = "blot Fun Blanker V" VERSION "." REVISION " for BTD";
  79.  
  80.  
  81. LONG MyBlankerLibInit(void)
  82.  
  83. {
  84.  if (GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L))
  85.   {
  86.    if (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L))
  87.     {
  88.      if (UtilityBase=OpenLibrary("utility.library",37L)) return TRUE;
  89.  
  90.      CloseLibrary (&IntuitionBase->LibNode);
  91.     }
  92.    CloseLibrary (&GfxBase->LibNode);
  93.   }
  94.  return FALSE;
  95. }
  96.  
  97. void MyBlankerLibFree(void)
  98.  
  99. {
  100.  CloseLibrary (UtilityBase);
  101.  CloseLibrary (&IntuitionBase->LibNode);
  102.  CloseLibrary (&GfxBase->LibNode);
  103. }
  104.  
  105.  
  106. typedef struct {
  107.   LONG  x,y;
  108. } XPoint;
  109.  
  110. #define MAXPOINTS 99
  111. #define MAX_DELTA 3
  112. #define MAX_blotS 2048
  113.  
  114. #define blot_THRESH 5
  115.  
  116. struct blotStruct
  117. {
  118.   struct BTDDrawInfo *BTDDrawInfo;
  119.   LONG   hs_RandN,hs_RandF,hs_RandI;
  120.   int width;
  121.   int height;
  122.   int xmid, ymid;
  123.   int offset;
  124.   int xsym, ysym;
  125.   int size;
  126.   int pix;
  127.   long startTime;
  128.   XPoint *pointBuffer;
  129.   int pointBufferSize;
  130.   int ncolors;
  131.   int seconds;
  132.   int zoom;
  133. };
  134.  
  135.  
  136. struct BTDInfo *QueryMyBlanker(void)
  137.  
  138. {
  139.  return &blotInfo;
  140. }
  141.  
  142.  
  143. void __regargs InitRandom(struct blotStruct *BP,ULONG Instance)
  144.  
  145. {
  146.  ULONG Time[2];
  147.  
  148.  CurrentTime (&Time[0],&Time[1]);
  149.  BP->hs_RandN=(LONG)Time[0];
  150.  if (Time[1]<1024L) Time[1]|=1;
  151.  else Time[1]>>=10;
  152.  Time[1]^=Instance;
  153.  
  154.  BP->hs_RandF=4*Time[1]+1;
  155.  BP->hs_RandI=2*Time[1]+1;
  156. }
  157.  
  158. WORD __regargs Random(struct blotStruct *BP,WORD Max)
  159.  
  160. {
  161.  BP->hs_RandN=BP->hs_RandF*BP->hs_RandN+BP->hs_RandI;
  162.  if (BP->hs_RandN<0L) BP->hs_RandN=-BP->hs_RandN;
  163.  
  164.  return (WORD)(BP->hs_RandN%Max);
  165. }
  166.  
  167.  
  168. #define FindTagData(l,t,d) GetTagData((t),(d),(l))
  169. /* rainbow colors */
  170.  
  171. #define NUM_RAINBOW_COLORS 6
  172.  
  173. UBYTE RBRed[NUM_RAINBOW_COLORS+1]   = {0xFF,0xFF,0x00,0x00,0x00,0xFF,0xFF};
  174. UBYTE RBGreen[NUM_RAINBOW_COLORS+1] = {0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00};
  175. UBYTE RBBlue[NUM_RAINBOW_COLORS+1]  = {0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00};
  176.  
  177. /* blot routine. */
  178.  
  179. struct blotStruct *InitMyBlanker(struct TagItem *TagList)
  180. {
  181.  struct blotStruct *BP;
  182.  struct BTDDrawInfo *BTDDrawInfo;
  183.  ULONG *Error,Dummy,Index,Instance;
  184.  ULONG Time[2];
  185.  
  186.  if ((BTDDrawInfo=(struct BTDDrawInfo *)
  187.                    FindTagData(TagList,BTD_DrawInfo,NULL))==NULL) return NULL;
  188.  Error=(LONG *)FindTagData(TagList,BTD_Error,(ULONG)&Dummy);
  189.  if ((BP=AllocVec(sizeof(struct blotStruct),MEMF_PUBLIC|MEMF_CLEAR))==NULL)
  190.   {
  191.    *Error=BTDERR_Memory;
  192.    return NULL;
  193.   }
  194.  
  195.  BP->BTDDrawInfo=BTDDrawInfo;
  196.  
  197.  BP->seconds=FindTagData(TagList,BP_Seconds,DEF_SECONDS);
  198.  BP->ncolors=FindTagData(TagList,BP_Colors,DEF_COLORS);
  199.  BP->zoom=FindTagData(TagList,BP_Zoom,DEF_ZOOM);
  200.  Instance=FindTagData(TagList,BTD_Instance,0L);
  201.  
  202.  InitRandom(BP,Instance);
  203.  
  204.  BP->width = BTDDrawInfo->BDI_Width-1;
  205.  BP->height = BTDDrawInfo->BDI_Height-1;
  206.  BP->xmid = BP->width / 2;
  207.  BP->ymid = BP->height / 2;
  208.  
  209.  BP->offset = 4;
  210.  BP->ysym = Random(BP,2);
  211.  BP->xsym = (BP->ysym) ? Random(BP,2) : 1;
  212.  BP->pix = Random(BP,BP->ncolors);
  213.  if (BP->offset <= 0) BP->offset = 3;
  214.  BP->size = BP->zoom * BP->width * BP->height / 1024;
  215.  
  216.  BP->pointBuffer = (XPoint *) AllocVec(BP->size * sizeof(XPoint),MEMF_PUBLIC|MEMF_CLEAR);
  217.  BP->pointBufferSize = BP->size * sizeof(XPoint);
  218.  
  219.  CurrentTime (&Time[0],&Time[1]);
  220.  BP->startTime = Time[0];
  221.  
  222.  if (BP->ncolors>NUM_RAINBOW_COLORS)
  223.   {
  224.    LONG ColNum,Col,RBCol;
  225.    UBYTE *Red,*Green,*Blue,*Changed;
  226.  
  227.    Red=BTDDrawInfo->BDI_Red;
  228.    Green=BTDDrawInfo->BDI_Green;
  229.    Blue=BTDDrawInfo->BDI_Blue;
  230.    Changed=BTDDrawInfo->BDI_Changed;
  231.    ColNum=BP->ncolors/NUM_RAINBOW_COLORS+1L;
  232.    Index=0L;
  233.    for (RBCol=0L; RBCol<NUM_RAINBOW_COLORS; RBCol++)
  234.     {
  235.      if (RBCol==(BP->ncolors%NUM_RAINBOW_COLORS)) ColNum--;
  236.  
  237.      for (Col=0L; Col<ColNum; Col++)
  238.       {
  239.        Red[BTDDrawInfo->BDI_Pens[Index]]=RBRed[RBCol]+((RBRed[RBCol+1]-RBRed[RBCol])*Col)/ColNum;
  240.        Green[BTDDrawInfo->BDI_Pens[Index]]=RBGreen[RBCol]+((RBGreen[RBCol+1]-RBGreen[RBCol])*Col)/ColNum;
  241.        Blue[BTDDrawInfo->BDI_Pens[Index]]=RBBlue[RBCol]+((RBBlue[RBCol+1]-RBBlue[RBCol])*Col)/ColNum;
  242.        Changed[BTDDrawInfo->BDI_Pens[Index++]]=TRUE;
  243.       }
  244.     }
  245.   }
  246.  else
  247.   for (Index=0L; Index<BP->ncolors; Index++)
  248.    {
  249.     BTDDrawInfo->BDI_Red[BTDDrawInfo->BDI_Pens[Index]]=RBRed[Index];
  250.     BTDDrawInfo->BDI_Green[BTDDrawInfo->BDI_Pens[Index]]=RBGreen[Index];
  251.     BTDDrawInfo->BDI_Blue[BTDDrawInfo->BDI_Pens[Index]]=RBBlue[Index];
  252.     BTDDrawInfo->BDI_Changed[BTDDrawInfo->BDI_Pens[Index]]=TRUE;
  253.    }
  254.  
  255. DEBUG_PRINT("blot: Init ready\n");
  256.  
  257.  return BP;
  258. }
  259.  
  260. void EndMyBlanker(struct blotStruct *BP)
  261.  
  262. {
  263. DEBUG_PRINT("blot: FreeMem\n");
  264.  if(BP->pointBuffer)
  265.   FreeVec(BP->pointBuffer);
  266.  FreeVec (BP);
  267. }
  268.  
  269. void XDrawPoints(struct blotStruct *BP,int size)
  270. {
  271.  int i,mx,my;
  272.  mx=BP->BTDDrawInfo->BDI_Width;
  273.  my=BP->BTDDrawInfo->BDI_Height;
  274.  for (i=0; i<size; i++)
  275.   if(BP->pointBuffer[i].x<mx && BP->pointBuffer[i].y<my &&
  276.      BP->pointBuffer[i].x>0  && BP->pointBuffer[i].y>0)
  277.   WritePixel(BP->BTDDrawInfo->BDI_RPort,BP->pointBuffer[i].x,BP->pointBuffer[i].y);
  278. }
  279.  
  280. void initblot(struct blotStruct *BP)
  281. {
  282.  
  283.  ULONG Time[2];
  284.  
  285.  BP->offset = 4;
  286.  BP->ysym = Random(BP,2);
  287.  BP->xsym = (BP->ysym) ? Random(BP,2) : 1;
  288.  BP->pix = Random(BP,BP->ncolors);
  289.  if (BP->offset <= 0) BP->offset = 3;
  290.  BP->size = BP->zoom * BP->width * BP->height / 1024;
  291.  
  292.  if (!BP->pointBuffer || BP->pointBufferSize < (BP->size * sizeof(XPoint))) 
  293.   {
  294.       if (BP->pointBuffer != NULL)
  295.         FreeVec(BP->pointBuffer);
  296.       BP->pointBuffer = (XPoint *) AllocVec(BP->size * sizeof(XPoint),MEMF_PUBLIC|MEMF_CLEAR);
  297.       BP->pointBufferSize = BP->size * sizeof(XPoint);
  298.   }
  299.  
  300.  CurrentTime (&Time[0],&Time[1]);
  301.  BP->startTime = Time[0];
  302.  
  303.  SetRast(BP->BTDDrawInfo->BDI_RPort,0);
  304.  
  305. }
  306.  
  307. void AnimMyBlanker(struct blotStruct *BP)
  308. {
  309.   int x, y;
  310.   int         k;
  311.   XPoint     *xp = BP->pointBuffer;
  312.   ULONG Time[2];
  313.  
  314.   SetAPen(BP->BTDDrawInfo->BDI_RPort,BP->BTDDrawInfo->BDI_Pens[BP->pix]);
  315.   if (++BP->pix>= BP->ncolors)
  316.       BP->pix = 0;
  317.  
  318.   x = BP->xmid;
  319.   y = BP->ymid;
  320.   k = BP->size;
  321.   while (k >= 4) {
  322.       x += (Random(BP,(1 + (BP->offset << 1))) - BP->offset);
  323.       y += (Random(BP,(1 + (BP->offset << 1))) - BP->offset);
  324.       k--;
  325.       xp->x = x;
  326.       xp->y = y;
  327.       xp++;
  328.       if (BP->xsym)
  329.     {
  330.           k--;
  331.       xp->x = BP->width - x;
  332.       xp->y = y;
  333.       xp++;
  334.     }
  335.       if (BP->ysym)
  336.     {
  337.           k--;
  338.       xp->x = x;
  339.       xp->y = BP->height - y;
  340.       xp++;
  341.     }
  342.       if (BP->xsym && BP->ysym)
  343.     {
  344.           k--;
  345.       xp->x = BP->width - x;
  346.       xp->y = BP->height - y;
  347.       xp++;
  348.     }
  349.     }
  350.     XDrawPoints (BP, BP->size - k);
  351.     CurrentTime (&Time[0],&Time[1]);
  352.     if (Time[0]- BP->startTime > BP->seconds)
  353.     initblot(BP);
  354. }
  355.  
  356. ULONG PenCountMyBlanker(struct TagItem *TagList)
  357.  
  358. {
  359.  ULONG colors;
  360.  colors=FindTagData(TagList,BP_Colors,DEF_COLORS);
  361.  return colors;
  362. }
  363.